//
//  SplitVideoView.m
//  Rail
//
//  Created by hychen on 2018/5/6.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SplitVideoView.h"

@implementation SplitVideoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        for (int i = 0; i < 4; ++i) {
            UIView *subView = [UIView new];
            [self addSubview:subView];
            self.videoViews = [NSMutableArray array];
            [self.videoViews addObject:subView];
        }
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
    CGFloat splitW = 0;
    CGFloat splitH = 0;
    switch (interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
            splitW = self.frame.size.height * 0.5;
            splitH = self.frame.size.width * 0.5;
        }
        default: {
            splitH = self.frame.size.height * 0.5;
            splitW = self.frame.size.width * 0.5;
        }
            break;
    }
    for (int i = 0; i < self.videoViews.count; ++i) {
        UIView *subV = [self.videoViews objectAtIndexSafe:i];
        subV.frame = CGRectMake(i%2*splitW, i/2*splitH, splitW, splitH);
    }
}

@end
